package com.sunwayhorizo.health.business.integration.messageImport;

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.jms.*;
import javax.ejb.*;
import org.jboss.seam.annotations.*;
import com.sunwayhorizo.health.business.dto.patad.encounter.*;
import com.sunwayhorizo.health.business.dto.registries.*;
import com.sunwayhorizo.health.business.integration.converter.*;
import com.sunwayhorizo.health.business.integration.model.*;
import com.sunwayhorizo.health.business.patad.empi.*;
import com.sunwayhorizo.health.business.patad.empi.query.*;
import com.sunwayhorizo.health.business.patad.encounter.*;
import com.sunwayhorizo.health.business.patad.encounter.query.*;
import com.sunwayhorizo.health.ets.exceptions.*;
import com.sunwayhorizo.health.hl7.common.*;
import com.sunwayhorizo.health.hl7.rim.*;

@Name("PatientMessageListener")
@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/patient"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), })
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class PatientMessageListener implements MessageListener
{
    @In(value = "AdmitManager", create = true)
    AdmitManagerLocal admitManager;

    @In(value = "ReceivingManager", create = true)
    ReceivingManagerLocal receivingManager;

    @In(value = "Converter", create = true)
    ConverterLocal converter;

    @In(value = "PatientManager", create = true)
    PatientManagerLocal patientManager;

    @In(value = "PatientQuery", create = true)
    PatientQueryLocal patientQuery;

    @In(value = "PatientEncounterQuery", create = true)
    PatientEncounterQueryLocal patientEncounterQuery;


    @Override
    public void onMessage(Message msg)

    {
        System.out.println("收取信息！");

        ObjectMessage objectMessage = (ObjectMessage) msg;
        try
        {

            @SuppressWarnings("unchecked")
            List<ArrayList<Serializable>> infoList = (List<ArrayList<Serializable>>) objectMessage.getObject();
            System.out.println("收取信息共有" + infoList.size() + "条");
            for (ArrayList<Serializable> infos : infoList)
            {

                PatientInfo patientInfo = (PatientInfo) infos.get(0);
                InHospitalInfo inHospitalInfo = (InHospitalInfo) infos.get(1);
                PatientStatusInfo patientStatusInfo = (PatientStatusInfo) infos.get(2);

                // 查询相关患者信息

                EntitysDTO entitysDTO = converter.convertToEntitysDTO(patientInfo);

                // 查询患者是否系统中已存在
                Patient patient = patientQuery.findPatientByNeturalId(patientStatusInfo.getNature_id());
                // 新建或者更新患者
                if (patient == null)
                {
                    patientManager.newPatientIndex(entitysDTO);
                }
                else
                {
                    patientManager.modifyPatient(entitysDTO);
                }
                // 查询入院信息
                Timestamp ts = inHospitalInfo.getAdmission_time();
                ts.setNanos(0);
                PatientEncounter inhos = patientEncounterQuery.findInhosBytime(ts, patientStatusInfo.getNature_id());
                if (inhos == null)
                {
                    // 组装入院dto
                    PatientEncounterDTO inhosDTO = converter.convertToPatientEncounterDTO(inHospitalInfo);
                    admitManager.newInHospital(inhosDTO);
                }
                // 组装入出转信息到dto
                PatientEncounterDTO dto = converter.convertToPatientEncounterDTO(patientStatusInfo);
                if (patientStatusInfo.getAction().equals("1"))
                {
                    receivingManager.newReceiving(dto);
                    converter.setReceivingDoctor(patientStatusInfo, dto);
                    receivingManager.newReceivingAdmit(dto);
                }
                else if (patientStatusInfo.getAction().equals("2"))
                {
                    admitManager.newDeptTransfer(dto);
                }
                else if (patientStatusInfo.getAction().equals("3"))
                {
                    admitManager.newOutHospital(dto);
                }

            }

        }
        catch (JMSException e)
        {
            e.printStackTrace();
        }
        catch (Hl7Exception e)
        {
            e.printStackTrace();
        }
        catch (ETSBadParameterException e)
        {
            e.printStackTrace();
        }
        catch (ETSDataException e)
        {
            e.printStackTrace();
        }
        catch (java.lang.IllegalStateException e)
        {
            e.printStackTrace();
        }

        catch (SecurityException e)
        {
            e.printStackTrace();
        }

    }
}
